package lp.xian.production.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lp.xian.production.domain.Product;
import lp.xian.production.mapper.ProductMapper;
import lp.xian.production.vo.OperationProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lp.xian.production.mapper.OperationProductMapper;
import lp.xian.production.domain.OperationProduct;
import lp.xian.production.service.IOperationProductService;

/**
 * 成品入库Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-06
 */
@Service
public class OperationProductServiceImpl implements IOperationProductService {
    @Autowired
    private OperationProductMapper operationProductMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询成品入库
     *
     * @param operationProductId 成品入库主键
     * @return 成品入库
     */
    @Override
    public OperationProduct selectOperationProductByOperationProductId(Integer operationProductId) {
        return operationProductMapper.selectOperationProductByOperationProductId(operationProductId);
    }

    /**
     * 查询成品入库列表
     *
     * @param operationProduct 成品入库
     * @return 成品入库
     */
    @Override
    public List<OperationProduct> selectOperationProductList(OperationProduct operationProduct) {
        return operationProductMapper.selectOperationProductList(operationProduct);
    }

    /**
     * 新增成品入库
     *
     * @param operationProduct 成品入库
     * @return 结果
     */
    @Override
    public int insertOperationProduct(OperationProduct operationProduct) {
        operationProduct.setCreateTime(DateUtils.getNowDate());
        return operationProductMapper.insertOperationProduct(operationProduct);
    }

    /**
     * 修改成品入库
     *
     * @param operationProduct 成品入库
     * @return 结果
     */
    @Override
    public int updateOperationProduct(OperationProduct operationProduct) {
        operationProduct.setUpdateTime(DateUtils.getNowDate());
        return operationProductMapper.updateOperationProduct(operationProduct);
    }

    /**
     * 批量删除成品入库
     *
     * @param operationProductIds 需要删除的成品入库主键
     * @return 结果
     */
    @Override
    public int deleteOperationProductByOperationProductIds(Integer[] operationProductIds) {
        return operationProductMapper.deleteOperationProductByOperationProductIds(operationProductIds);
    }

    /**
     * 删除成品入库信息
     *
     * @param operationProductId 成品入库主键
     * @return 结果
     */
    @Override
    public int deleteOperationProductByOperationProductId(Integer operationProductId) {
        return operationProductMapper.deleteOperationProductByOperationProductId(operationProductId);
    }

    @Override
    public int chonseOperationProduct(OperationProductVo operationProductvo) {
        OperationProduct operationProduct = new OperationProduct();
        operationProduct.setOperationProductId(operationProductvo.getOperationProductId());
        operationProduct.setWarehouseId(Integer.parseInt(operationProductvo.getWarehouseId()));
        operationProduct.setShelfId(Integer.parseInt(operationProductvo.getShelfId()));
        operationProduct.setLocationId(Integer.parseInt(operationProductvo.getLocationId()));
        return operationProductMapper.updateOperationProduct(operationProduct);
    }

    @Override
    public int auditOperationProduct(OperationProductVo operationProductVo) {
        OperationProduct operationProduct = new OperationProduct();
        operationProduct.setOperationProductId(operationProductVo.getOperationProductId());
        operationProduct.setAuditStatus(Integer.parseInt(operationProductVo.getAuditStatus()));
        operationProduct.setAuditOpinion(operationProductVo.getAuditOpinion());
        operationProduct.setAuditPerson(SecurityUtils.getUsername());
        operationProduct.setAuditTime(new Date());
        return operationProductMapper.updateOperationProduct(operationProduct);
    }

    @Override
    public int approverOperationproduct(OperationProductVo operationProductVo) {
        OperationProduct operationProduct = new OperationProduct();
        operationProduct.setOperationProductId(operationProductVo.getOperationProductId());
        operationProduct.setAuditStatus(Integer.parseInt(operationProductVo.getAuditStatus()));
        operationProduct.setApproverOpinion(operationProductVo.getAuditOpinion());
        operationProduct.setApproverPerson(SecurityUtils.getUsername());
        operationProduct.setApproverTime(new Date());
        int num = operationProductMapper.updateOperationProduct(operationProduct);
        if (num == 1) {
            OperationProduct op = operationProductMapper.selectOperationProductByOperationProductId(operationProductVo.getOperationProductId());
            if (op.getAuditStatus().intValue() == 2) {
                Product product = new Product();
                //根据成品列表名称 获取id
                Product product1 = productMapper.selectProductByProductName(op.getOperationProductName());
                //如果成品列表存在 则添加数量
                if (op.getOperationProductName().equals(product1.getProductName())) {
                    int productId = product1.getProductId();
                    product.setProductId(productId);
                    int proNum = product1.getProductNum() + op.getOperationProductNum();
                    product.setProductNum(proNum);
                    product.setUpdateBy(SecurityUtils.getUsername());
                    product.setUpdateTime(new Date());
                    return productMapper.updateProduct(product);
                    //成品名称不存在 添加新的库存
                } else {
                    product.setProductName(op.getOperationProductName());
                    product.setProductNum(op.getOperationProductNum());
                    product.setProductUnit(op.getOperationProductUnit());
                    product.setOperationProductId(op.getOperationProductId());
                    product.setProductPrice(op.getOperationProductPrice());
                    product.setCreateBy(SecurityUtils.getUsername());
                    product.setCreateTime(new Date());
                    return productMapper.insertProduct(product);
                }
            }

        }
        return 1;
    }
}
